package com.smashcrush.client.gwt;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.smashcrush.client.gwt.service.UserServiceAsync;
import com.smashcrush.client.gwt.util.WidgetUtil;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class RegistrationApplication implements EntryPoint {

	DockPanel dock;

	TextBox emailAddressTextBox;

	TextBox confirmEmailAddressTextBox;

	PasswordTextBox passwordTextBox;

	PasswordTextBox confirmPasswordTextBox;

	Button registerButton;

	public void onModuleLoad() {
		dock = new DockPanel();
		dock.setSize("100%", "100%");

		FlexTable table = new FlexTable();
		dock.add(table, DockPanel.CENTER);

		emailAddressTextBox = new TextBox();
		table.setWidget(0, 0, WidgetUtil.createLabel("E-mail Address"));
		table.setWidget(0, 1, emailAddressTextBox);

		confirmEmailAddressTextBox = new TextBox();
		table.setWidget(1, 0, WidgetUtil.createLabel("Confirm E-mail Address"));
		table.setWidget(1, 1, confirmEmailAddressTextBox);

		passwordTextBox = new PasswordTextBox();
		table.setWidget(2, 0, WidgetUtil.createLabel("Password"));
		table.setWidget(2, 1, passwordTextBox);

		confirmPasswordTextBox = new PasswordTextBox();
		table.setWidget(3, 0, WidgetUtil.createLabel("Confirm Password"));
		table.setWidget(3, 1, confirmPasswordTextBox);

		registerButton = new Button("Register");
		dock.add(registerButton, DockPanel.SOUTH);
		registerButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				register();
			}
		});

		RootPanel.get().add(dock);
	}

	private void register() {

		String emailAddress = emailAddressTextBox.getValue();
		String password = passwordTextBox.getValue();
		System.out.println("ehzzzzz");
		UserServiceAsync.RPC.get().register(emailAddress, password,
				new AsyncCallback() {

					@Override
					public void onSuccess(Object result) {
						Window.alert("success!");
					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}
				});
	}

}
